import Vue from 'vue'
import App from './home/app'
import axios from 'axios'
import VueRouter from 'vue-router'
import Routers from './routers'
Vue.use(VueRouter)
/**
 * 自定义配置
 * @type {Object|*}
 */
var axiosInstance = axios.create();
axiosInstance.defaults.baseURL = configM.comUrl;
axiosInstance.defaults.timeout = 10000;
axiosInstance.defaults.headers.post['content-type'] = 'application/json;charset=utf-8';
// Add a request interceptor请求拦截
axiosInstance.interceptors.request.use(function (config) {
  Loading.open('加载中...');
  // Do something before request is sent
  if(config.url.indexOf('/session/') == -1){
    config.headers.sessionId = Util.sessionId();
  }
  return config;
}, function (error) {
  Loading.close('加载中...')
  // Do something with request error
  return Promise.reject(error);
});

// Add a response interceptor请求响应拦截
axiosInstance.interceptors.response.use(function (response) {
  Loading.close('加载中...')
  // Do something with response data
  if(response.data.meta.code == 'CIF5000') {
    ErrorPage.show({type: 2});
        setTimeout(function () {
          var params = { "functionType": 1 };
          window.location.href = "toon://mwap/window?params=" + JSON.stringify(params);
        }, 5000)
  }

  if(response.data.meta.code != 'CIF5000' && response.data.meta.code !='0') {
      ErrorPage.show();
  }
  return response;
}, function (error) {
  ErrorPage.show();
  Loading.close('加载中...')
  if(error.response){
    console.log(error.response.data);
    console.log(error.response.status);
    console.log(error.response.headers);
  }else{
    // console.log('Error',error.message);
  }
  console.log(error.config);
  // Do something with response error
  return Promise.reject(error);
});

Vue.prototype.$http = axiosInstance
Vue.use(VueRouter)

Vue.config.debug = false
Vue.config.devtools = false
Vue.config.silent = true
//
Object.defineProperty(Vue.prototype, '$util', {value: Util});
window.Vue = Vue;
// 并且配置路由规则
const router = new VueRouter({
  mode: 'hash',
  routes: Routers
})

router.beforeEach((to, from, next) => {
  Loading.close();
  next();
})

router.afterEach(route  => {
  //解决 其他页面加载更多的问题
  Util.moreAndMoreOnscroll(false);
  $('#MoreAndMore_NoDataMsg').hide();
  ErrorPage.close();
})

// 全局配置
Vue.config.debug = false
Vue.config.devtools = false
Vue.config.silent = true


/* eslint-disable no-new */
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
